//
//  LampColorSetUpView.m
//  YIJU2.0-HD
//
//  Created by 陈柏兵 on 16/5/10.
//  Copyright © 2016年 张金国. All rights reserved.
//

#import "ModelLampColorSetUpView.h"
#import "Macro.h"
#import "Masonry.h"
#import "UIButton+ColorandText.h"

#define color_one   [UIColor colorWithRed:176/255.0 green:66/255.0 blue:62/255.0 alpha:1.000]
#define color_two   [UIColor colorWithRed:232/255.0 green:149/255.0 blue:32/255.0 alpha:1.000]
#define color_three [UIColor colorWithRed:189/255.0 green:186/255.0 blue:52/255.0 alpha:1.000]
#define color_four  [UIColor colorWithRed:65/255.0 green:160/255.0 blue:46/255.0 alpha:1.000]
#define color_five  [UIColor colorWithRed:69/255.0 green:160/255.0 blue:170/255.0 alpha:1.000]
#define color_six   [UIColor colorWithRed:66/255.0 green:108/255.0 blue:207/255.0 alpha:1.000]
#define color_seven [UIColor colorWithRed:202/255.0 green:61/255.0 blue:120/255.0 alpha:1.000]
#define color_eight [UIColor colorWithRed:151/255.0 green:95/255.0 blue:46/255.0 alpha:1.000]
#define color_nine  [UIColor colorWithRed:81/255.0 green:69/255.0 blue:175/255.0 alpha:1.000]
#define color_ten   [UIColor colorWithRed:74/255.0 green:173/255.0 blue:115/255.0 alpha:1.000]


@interface ModelLampColorSetUpView()
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) NSArray *colorArray;
@end

@implementation ModelLampColorSetUpView
#pragma mark - init
- (instancetype)initWithLampColor:(ModelLampColorSetUpTypeBlock)lampColor {
    if (self = [super init]) {
        self.lampColorBlock = lampColor;
        [self setupConstraints];
    }
    return self;
}

- (void)setupConstraints {
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top);
        make.left.equalTo(self.mas_left);
        make.width.equalTo(@50);
        make.bottom.equalTo(self.mas_bottom).offset(-TenOffsetViewMargin);
    }];
    
    [self colorArray];
    UIButton *tempBtn = nil;
    for (int i = 0; i <10; i ++) {
        UIButton *colorBtn = [UIButton buttonWithBackgroundColor:_colorArray[i] textFont:nil textColor:nil title:nil cornerRadius:5];
        colorBtn.tag = 500 + i;
        [colorBtn addTarget:self action:@selector(handleColorAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:colorBtn];
        if (tempBtn == nil) {
            [colorBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.mas_top);
                make.left.equalTo(_titleLabel.mas_right).offset(TweentyOffsetViewMargin);
                make.width.equalTo(self.mas_width).multipliedBy(1/18.0f);
                make.height.equalTo(self.mas_width).multipliedBy(1/18.0f);
            }];
        }else {
            [colorBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.mas_top);
                make.left.equalTo(tempBtn.mas_right).offset(TweentyOffsetViewMargin);
                make.width.equalTo(self.mas_width).multipliedBy(1/18.0f);
                make.height.equalTo(self.mas_width).multipliedBy(1/18.0f);
            }];
        }
        tempBtn = colorBtn;
    }
}

- (UILabel *)titleLabel {
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textAlignment = NSTextAlignmentRight;
        _titleLabel.font = kTitle_Font;
        _titleLabel.text = @"颜色:";
        _titleLabel.textColor = [UIColor blackColor];
        [self addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (NSArray *)colorArray {
    if (_colorArray == nil) {
        _colorArray = @[color_one,color_two,color_three,color_four,color_five,color_six,color_seven,color_eight,color_nine,color_ten];
    }
    return _colorArray;
}

- (void)handleColorAction:(UIButton *)button {
    NSInteger btnTag = button.tag - 500;
    ModelLampColorSetUpType ModelLampColorSetUpType = btnTag;
    self.lampColorBlock(ModelLampColorSetUpType);
}

@end
